<template>
	<view>
		<scroll-view  class="more-warp" scroll-y="true" scroll-x="false" @scrolltolower="onscrolltolower">
			<movieGridTemplate :list="list"></movieGridTemplate>
		</scroll-view>
	</view>
</template>

<script>
	import movieGridTemplate from "../movie-grid-template/movie-grid-template.vue";
	import Config from "../../../static/model/config.js";
	import util from "../../../static/model/util.js";
	export default {
		data(){
			return{
				title:'',
				api:Config.api,
				url:'',
				list:{},
				totalCount:0,
				requestUrl:'',
				isEmpty:true
			}
		},
		methods:{
			precessDoubanData(moviesDouban){
				var movies=[];
				for(var idx in moviesDouban.subjects){
					var subject=moviesDouban.subjects[idx];
					var title=subject.title;
					if(title.length>=6){
						title=title.substring(0,6)+'...';
					}
					var temp={
						title:title,
						average:subject.rating.average,
						coverageUrl:subject.images.large,
						movieId:subject.id
					}	
					movies.push(temp);
				}
				//下拉加载的数值累加
				var totalMovies={};
				if(!this.isEmpty){
					totalMovies=this.list.concat(movies);
				}else{
					totalMovies=movies;
					this.isEmpty=false;
				};
				this.totalCount+=20;
				this.list=totalMovies;
				uni.hideNavigationBarLoading();
				uni.stopPullDownRefresh();
			},
			onscrolltolower(e){
				var nextUrl=this.requestUrl+"?start="+this.totalCount+"&count=20";
				util.http(nextUrl,"GET",this.precessDoubanData);
				uni.showNavigationBarLoading();
			},
			//下拉事件
			onPullDownRefresh() {
				var refreshURL=this.requestUrl+'?start=0&count=20';
				this.list={};
				this.isEmpty=true;
				util.http(refreshURL,"GET",this.precessDoubanData);
				uni.showNavigationBarLoading();
			}
// 			ontouchpull(e){
				// util.PullToRefresh(e);
// 			}
		},
		mounted(){		
			util.http(this.url,"GET",this.precessDoubanData)
		},
		onLoad(options){//加载时接受传递过来的url值
			this.title=options.category;
			switch(this.title){	
				case "即将上映":
					this.url=this.api+'/v2/movie/in_theaters';
					break;
				case "正在热映":
					this.url=this.api+'/v2/movie/coming_soon';
					break;
				case "豆瓣TOP250":
					this.url=this.api+'/v2/movie/top250';
					break;				
			};
			this.requestUrl=this.url;
			uni.setNavigationBarTitle({
				title:this.title
			})
		},
		components:{//实例化模板
			movieGridTemplate
		}
	}
</script>

<style>
	.more-warp{height: 1300rpx;background-color: #fff;}
</style>
